/** 对axios做一些配置 **/

// import { baseUrl } from "../config";
import { baseUrl } from "@/util/api/index";
import axios from "axios";
import store from "@/store";
import { message } from "antd";

/**
 * MOCK模拟数据
 * 不需要下面这些mock配置，仅本地用
 * 正式打包需要去掉
 * */
// import Mock from "mockjs";
// @ts-ignore
// import mock from "../../mock/app-data.js";
// Mock.mock(/\/api.*/, (options: any) => {
//   const res = mock(options);
//   return res;
// });
/* The line `import mock from "../../mock/app-data.js";` is importing the `app-data.js` file from the
`mock` directory. This file contains mock data that is used for testing or development purposes. */

/**
 * 根据不同环境设置不同的请求地址
 * 把返回值赋给axios.defaults.baseURL即可
 */
// function setBaseUrl(){
//   switch(process.env.NODE_ENV){
//     case 'development': return 'http://development.com';
//     case 'test': return 'http://test.com';
//     case 'production' : return 'https://production.com';
//     default : return baseUrl;
//   }
// }

// 默认基础请求地址
axios.defaults.baseURL = baseUrl;
// 请求是否带上cookie
axios.defaults.withCredentials = false;
// 发送请求前
axios.interceptors.request.use((config) => {
  console.log("发送请求：", config);
  const userinfo = store.getState().app.userinfo.userBasicInfo || {};
  config.headers.Authorization = userinfo.token || "";
  return config;
});

// 对返回的结果做处理
axios.interceptors.response.use((response) => {
  const code = response?.data?.code ?? 200;
  // 没有权限，登录超时，登出，跳转登录
  if (code === 403) {
    message.error("登录超时，请重新登录");
    sessionStorage.removeItem("userinfo");
    setTimeout(() => {
      window.location.href = "/";
    }, 1500);
  } else {
    return response.data;
  }
  return response.data;
});

export default axios;
